package com.tlias;

import com.tlias.interceptor.DemoInterceptor;
import com.tlias.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration//声明当前类是一个配置类等价@Component,是拦截器的配置类
//需要实现WebMvcConfigurer
/**
 * Web配置类，用于注册拦截器
 */
public class Webconfig implements WebMvcConfigurer {
    // 自动注入DemoInterceptor拦截器
    @Autowired
    private DemoInterceptor demoInterceptor;
    @Autowired
    private LoginInterceptor loginInterceptor;

    /**
     * 重写addInterceptors方法，注册自定义拦截器
     * @param registry 拦截器注册对象
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns为所有路径添加拦截器,excludePathPatterns为除了不拦截/login
        // /**代表匹配所有,格式比过滤器多了一个*,如果这个是/*就只能匹配一级里的任意路径
//        registry.addInterceptor(demoInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
    }
}
